package com.ruoyi.questionnaire.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.questionnaire.domain.QuestionnaireOption;
import com.ruoyi.questionnaire.domain.QuestionnaireQuestion;
import com.ruoyi.questionnaire.mapper.QuestionnaireOptionMapper;
import com.ruoyi.questionnaire.mapper.QuestionnaireQuestionMapper;
import com.ruoyi.questionnaire.service.IQuestionnaireQuestionService;

@Service
public class QuestionnaireQuestionServiceImpl implements IQuestionnaireQuestionService {
    @Autowired
    private QuestionnaireQuestionMapper questionMapper;
    
    @Autowired
    private QuestionnaireOptionMapper optionMapper;

    @Override
    public QuestionnaireQuestion selectQuestionnaireQuestionById(Long id) {
        return questionMapper.selectQuestionnaireQuestionById(id);
    }

    @Override
    public List<QuestionnaireQuestion> selectQuestionnaireQuestionList(Long questionnaireId) {
        List<QuestionnaireQuestion> questions = questionMapper.selectQuestionnaireQuestionList(questionnaireId);
        
        // 为每个题目加载选项信息
        for (QuestionnaireQuestion question : questions) {
            if (question != null && question.getId() != null) {
                List<QuestionnaireOption> options = optionMapper.selectQuestionnaireOptionList(question.getId());
                question.setOptionList(options);
            }
        }
        
        return questions;
    }

    @Override
    public List<QuestionnaireQuestion> selectAllQuestionnaireQuestionList() {
        List<QuestionnaireQuestion> questions = questionMapper.selectAllQuestionnaireQuestionList();
        
        // 为每个题目加载选项信息
        for (QuestionnaireQuestion question : questions) {
            if (question != null && question.getId() != null) {
                List<QuestionnaireOption> options = optionMapper.selectQuestionnaireOptionList(question.getId());
                question.setOptionList(options);
            }
        }
        
        return questions;
    }

    @Override
    @Transactional
    public int insertQuestionnaireQuestion(QuestionnaireQuestion question) {
        question.setCreateTime(new Date());
        question.setUpdateTime(new Date());
        System.out.println("=== 新增题目调试信息 ===");
        System.out.println("题目ID: " + question.getId());
        System.out.println("题目内容: " + question.getContent());
        System.out.println("题目类型: " + question.getQuestionType());
        System.out.println("选项列表: " + question.getOptionList());
        System.out.println("选项数量: " + (question.getOptionList() != null ? question.getOptionList().size() : 0));
        
        // 1. 先保存题目主表
        int result = questionMapper.insertQuestionnaireQuestion(question);
        System.out.println("题目保存结果: " + result);
        System.out.println("保存后题目ID: " + question.getId()); // 检查主键是否回填
        
        // 2. 如果题目保存成功且有选项数据，则保存选项
        if (result > 0 && question.getOptionList() != null && !question.getOptionList().isEmpty()) {
            System.out.println("开始保存选项...");
            Long questionId = question.getId(); // 获取回填的主键ID
            System.out.println("使用题目ID: " + questionId);
            
            for (int i = 0; i < question.getOptionList().size(); i++) {
                QuestionnaireOption option = question.getOptionList().get(i);
                option.setQuestionId(questionId); // 使用正确的题目ID
                option.setOrderNum(i + 1);
                
                // 确保optionValue不为空
                if (option.getOptionValue() == null || option.getOptionValue().trim().isEmpty()) {
                    option.setOptionValue(option.getOptionLabel()); // 如果optionValue为空，使用optionLabel
                }
                
                System.out.println("保存选项 " + (i + 1) + ": " + option.getOptionLabel() + ", ID: " + questionId);
                int optionResult = optionMapper.insertQuestionnaireOption(option);
                System.out.println("选项保存结果: " + optionResult);
            }
        } else {
            System.out.println("没有选项数据或题目保存失败");
        }
        
        return result;
    }

    @Override
    @Transactional
    public int updateQuestionnaireQuestion(QuestionnaireQuestion question) {
        System.out.println("=== 更新题目调试信息 ===");
        System.out.println("题目ID: " + question.getId());
        System.out.println("题目内容: " + question.getContent());
        System.out.println("选项数量: " + (question.getOptionList() != null ? question.getOptionList().size() : 0));
        
        int result = questionMapper.updateQuestionnaireQuestion(question);
        System.out.println("题目更新结果: " + result);
        
        // 如果题目更新成功且有选项数据，则更新选项
        if (result > 0 && question.getOptionList() != null) {
            System.out.println("开始更新选项...");
            Long questionId = question.getId();
            
            // 先删除该题目的所有选项
            optionMapper.deleteQuestionnaireOptionByQuestionId(questionId);
            System.out.println("删除旧选项完成");
            
            // 然后添加新选项
            for (int i = 0; i < question.getOptionList().size(); i++) {
                QuestionnaireOption option = question.getOptionList().get(i);
                option.setQuestionId(questionId);
                option.setOrderNum(i + 1);
                
                // 确保optionValue不为空
                if (option.getOptionValue() == null || option.getOptionValue().trim().isEmpty()) {
                    option.setOptionValue(option.getOptionLabel());
                }
                
                System.out.println("更新选项 " + (i + 1) + ": " + option.getOptionLabel());
                int optionResult = optionMapper.insertQuestionnaireOption(option);
                System.out.println("选项更新结果: " + optionResult);
            }
        } else {
            System.out.println("没有选项数据或题目更新失败");
        }
        
        return result;
    }

    @Override
    public int deleteQuestionnaireQuestionById(Long id) {
        return questionMapper.deleteQuestionnaireQuestionById(id);
    }

    @Override
    @Transactional
    public int deleteQuestionnaireQuestionByIds(Long[] ids) {
        int totalDeleted = 0;
        for (Long id : ids) {
            // 先删除该题目的所有选项
            optionMapper.deleteQuestionnaireOptionByQuestionId(id);
            // 再删除题目
            totalDeleted += questionMapper.deleteQuestionnaireQuestionById(id);
        }
        return totalDeleted;
    }

    @Override
    public int deleteQuestionnaireQuestionByQuestionnaireId(Long questionnaireId) {
        return questionMapper.deleteQuestionnaireQuestionByQuestionnaireId(questionnaireId);
    }

    @Override
    public Integer getMaxOrderNumByQuestionnaireId(Long questionnaireId) {
        return questionMapper.getMaxOrderNumByQuestionnaireId(questionnaireId);
    }

    @Override
    public QuestionnaireQuestion loadQuestionWithOptions(Long questionId) {
        QuestionnaireQuestion question = questionMapper.selectQuestionnaireQuestionById(questionId);
        if (question != null) {
            // 加载该题目的选项
            List<QuestionnaireOption> options = optionMapper.selectQuestionnaireOptionList(questionId);
            question.setOptionList(options);
        }
        return question;
    }
} 